package com.hm.ipay.service;

import com.hm.ipay.dao.*;
import com.hm.ipay.domain.result.ExceptionMsg;
import com.hm.ipay.domain.result.ResponseData;
import com.hm.ipay.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {

    @Autowired
    AdminUserMapper adminUserMapper;

    @Autowired
    GlobalConfigMapper globalConfigMapper;

    @Autowired
    ValidateMapper validateMapper;

    @Autowired
    UserReturnVisitMapper userReturnVisitMapper;

    public ResponseData adminUserList() {
        List<AdminUser> accounts = adminUserMapper.selectAll(0L);
        return new ResponseData(ExceptionMsg.SUCCESS, accounts);
    }

    public ResponseData adminUserLogin(AdminUser adminUser, String val) {
        AdminUser account = adminUserMapper.selectByAdminName(adminUser.getAdminName());
        if (account == null) {
            return new ResponseData(ExceptionMsg.AdminUserNotFound);
        } else if (!adminUser.getPassword().equals(account.getPassword())) {
            return new ResponseData(ExceptionMsg.AdminUserPasswordError);
        }
        String validate = validateMapper.selectCode(val);
        if (validate == null) {
            return new ResponseData(ExceptionMsg.ValidateError);
        }
        validateMapper.clearValidate();
        return new ResponseData(ExceptionMsg.SUCCESS, account);
    }

    public ResponseData adminUserDelete(AdminUser adminUser) {
        adminUserMapper.deleteByPrimaryKey(adminUser.getId());
        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    public ResponseData adminUserAdd(AdminUser adminUser) {
        adminUserMapper.insertSelective(adminUser);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    public ResponseData adminUserUpdate(AdminUser adminUser) {
        adminUserMapper.updateByPrimaryKeySelective(adminUser);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    public ResponseData getAdminUser(Long id) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(id);
        return new ResponseData(ExceptionMsg.SUCCESS, adminUser);
    }

    public ResponseData getSystemConfig() {
        GlobalConfig config = globalConfigMapper.selectByPrimaryKey(1L);
        return new ResponseData(ExceptionMsg.SUCCESS, config);
    }

    public ResponseData updateSystemConfig(GlobalConfig config) {
        globalConfigMapper.updateByPrimaryKeySelective(config);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    public ResponseData addUserReturnVisit(UserReturnVisit returnVisit) {
        userReturnVisitMapper.insertSelective(returnVisit);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }
}
